package com.chenglu.android.goodsinfo;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chenglu.android.R;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.cart.bean.CartBean;
import com.chenglu.android.common.base.BaseActionBarActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.goodsinfo.adapter.CommentAdapter;
import com.chenglu.android.goodsinfo.bean.GoodsCommentBean;
import com.chenglu.android.goodsinfo.bean.GoodsInfoBean;
import com.chenglu.android.goodsinfo.bean.ShopCouponPage;
import com.chenglu.android.goodsinfo.bean.ShopsinfoBean;
import com.chenglu.android.goodsinfo.mvp.contract.GoodsInfoContract;
import com.chenglu.android.goodsinfo.mvp.presenter.GoodsInfoPresenter;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.payment.bean.CouponListBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: lyj
 * @Created: by Copyright 2023 3 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class CommentActivity extends BaseActionBarActivity<GoodsInfoPresenter> implements GoodsInfoContract.View {

    public static final String GoodsId = "GoodsId";
    @BindView(R.id.rv_comment)
    RecyclerView rvComment;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    private CommentAdapter commentAdapter;

    int current = 1;
    private String goodsId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comment;
    }


    @Override
    protected void initView() {
        setCenterText("商品评价");
        mPresenter = new GoodsInfoPresenter();
        mPresenter.attachView(this);

        goodsId = getIntent().getStringExtra(GoodsId);

        mPresenter.getGoodsComment(getMapCommentParams());
        initCommentRv();
//        //刷新猜你喜欢的数据
        initRefreshLayout();

    }


    private Map<String, String> getMapCommentParams() {
        Map<String, String> map = new HashMap<>();
        map.put("current", current + "");
        map.put("goodsId", goodsId);
//        map.put("goodsReview", id);
        map.put("size", Constant.SIZE + "");

        return map;
    }

    //刷新完成
    private void finishRefresh(RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    //刷新
    private void initRefreshLayout() {

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                commentBeanList.clear();
                mPresenter.getGoodsComment(getMapCommentParams());


            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                mPresenter.getGoodsComment(getMapCommentParams());

            }
        });
    }

    private void initCommentRv() {
        commentAdapter = new CommentAdapter(mContext, commentBeanList);
        rvComment.setLayoutManager(new LinearLayoutManager(mContext));
        rvComment.setAdapter(commentAdapter);
//        commentAdapter.notifyDataSetChanged();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    List<GoodsCommentBean.Data.Records> commentBeanList = new ArrayList<>();


    @Override
    public void getGoodsInfo(GoodsInfoBean bean) {

    }

    @Override
    public void getGoodsComment(GoodsCommentBean bean) {
//刷新完成
        finishRefresh(smartrefreshlayout);
        if (bean.getData().getSize() > 0) {
            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                //添加集合中banner的数据
                commentBeanList.add(bean.getData().getRecords().get(i));
            }
            if (bean.getData().getTotal() < Constant.SIZE) {
                smartrefreshlayout.setEnableRefresh(false);
            }
            if (current * Constant.SIZE > bean.getData().getTotal()) {
                smartrefreshlayout.setEnableLoadMore(false);
                View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.color_f6f6f6));
                commentAdapter.addFooterView(inflate);
            }
        } else {
            showEmptyView("暂无数据");
        }

        commentAdapter.notifyDataSetChanged();
    }

    @Override
    public void getShopinfo(ShopsinfoBean bean) {

    }

    @Override
    public void getGoodsCollectRemove(SmsCodeBean bean) {

    }

    @Override
    public void getShopsCollectRemove(SmsCodeBean bean) {

    }

    @Override
    public void getGoodsCollectSave(SmsCodeBean bean) {

    }

    @Override
    public void getShopsRecommendList(IndexRecomBean bean) {

    }

    @Override
    public void getAddCartShop(CommonDataBean bean) {

    }

    @Override
    public void getShopsCart(CartBean bean) {

    }

    @Override
    public void getShopCouponPage(ShopCouponPage bean) {

    }

    @Override
    public void getShopCouponreceive(CommonDataBean bean) {

    }


}
